package com.studentmanagement.ui;

import com.studentmanagement.model.Course;
import com.studentmanagement.model.User;
import com.studentmanagement.service.CourseService;
import com.studentmanagement.service.TeacherService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class GradeEntryPanel extends JPanel {
    private User currentUser;
    private CourseService courseService;
    private TeacherService teacherService;
    
    private JTable courseTable;
    private DefaultTableModel tableModel;
    private JButton enterGradesButton;
    private JButton refreshButton;

    public GradeEntryPanel(User user) {
        this.currentUser = user;
        this.courseService = new CourseService();
        this.teacherService = new TeacherService();
        
        initComponents();
        setupLayout();
        setupListeners();
        loadCourseData();
    }

    private void initComponents() {
        // 表格列名
        String[] columnNames = {"课程代码", "课程名称", "学分", "上课时间", "教室", "已选人数", "容量"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        courseTable = new JTable(tableModel);
        courseTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        courseTable.getTableHeader().setReorderingAllowed(false);
        courseTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        courseTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        courseTable.setRowHeight(30);
        
        // 按钮初始化
        enterGradesButton = new JButton("录入成绩");
        refreshButton = new JButton("刷新");
        
        // 设置按钮字体
        Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
        enterGradesButton.setFont(buttonFont);
        refreshButton.setFont(buttonFont);
    }

    private void setupLayout() {
        setLayout(new BorderLayout(10, 10));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建标题面板
        JPanel titlePanel = new JPanel();
        JLabel titleLabel = new JLabel("成绩录入");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titlePanel.add(titleLabel);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(enterGradesButton);
        buttonPanel.add(refreshButton);

        // 创建北部面板，包含标题和按钮
        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(titlePanel, BorderLayout.WEST);
        northPanel.add(buttonPanel, BorderLayout.EAST);

        // 添加组件到主面板
        add(northPanel, BorderLayout.NORTH);
        add(new JScrollPane(courseTable), BorderLayout.CENTER);
    }

    private void setupListeners() {
        enterGradesButton.addActionListener(e -> onEnterGrades());
        refreshButton.addActionListener(e -> loadCourseData());
    }

    private void loadCourseData() {
        tableModel.setRowCount(0);
        // 获取当前教师ID
        Integer teacherId = teacherService.getTeacherByUserId(currentUser.getId()).getId();
        // 获取教师教授的课程
        List<Course> courses = courseService.getCoursesByTeacherId(teacherId);
        
        for (Course course : courses) {
            Object[] rowData = {
                course.getCourseCode(),
                course.getName(),
                course.getCredits(),
                course.getSchedule(),
                course.getClassroom(),
                courseService.getEnrolledStudentCount(course.getId()),
                course.getCapacity()
            };
            tableModel.addRow(rowData);
        }
    }

    private void onEnterGrades() {
        int selectedRow = courseTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请先选择一门课程！", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String courseCode = (String) tableModel.getValueAt(selectedRow, 0);
        Course course = courseService.getCourseByCourseCode(courseCode);
        
        if (course != null) {
            // 打开成绩管理面板，传入课程ID
            JFrame frame = new JFrame("学生成绩管理 - " + course.getName());
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(this);
            
            // 创建一个特定课程的成绩管理面板
            GradeManagementPanel panel = new GradeManagementPanel();
            frame.add(panel);
            
            frame.setVisible(true);
        }
    }
}